﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using A2.ERP.BLL;
using A2.ERP.Models;

namespace A2.ERP.UI.Controllers
{
    public class PurchaseController : Controller
    {
        //
        // GET: /Purchase/

        #region server
        private readonly IBLL.It_StoreHouseService _tStoreHouse = DBService.GetService<t_StoreHouseService>();
        private readonly IBLL.It_StoreHouseDetailService _tStoreHouseDetail = DBService.GetService<t_StoreHouseDetailService>();
        private readonly IBLL.It_BuyOrderService _tBuyOrderService = DBService.GetService<t_BuyOrderService>();
        private readonly IBLL.It_BuyReceiptService _tBuyReceiptService = DBService.GetService<t_BuyReceiptService>();
        private readonly IBLL.It_UserService _tUserService = DBService.GetService<t_UserService>();
        private readonly IBLL.It_BuyPayService _tBuyPayService = DBService.GetService<t_BuyPayService>();
        private readonly IBLL.It_SupplierService _tSupplierService = DBService.GetService<t_SupplierService>();
        private readonly IBLL.It_ProductsService _tProductsService = DBService.GetService<t_ProductsService>();
        private readonly IBLL.It_BuyReturnService _tBuyReturnService = DBService.GetService<t_BuyReturnService>();
        private readonly IBLL.It_AppendStockDetailService _tAppendStockDetailService = DBService.GetService<t_AppendStockDetailService>();
        #endregion

        /// <summary>
        /// 采购订单页面
        /// </summary>
        /// <returns></returns>

        public ActionResult ModuleBuyOrder(int id = 0)
        {
            //查找订单信息
            ViewBag.Order = _tBuyOrderService.LoadEntities(i => true).ToList().LastOrDefault();
            //供应商
            ViewBag.supplier = _tSupplierService.LoadEntities(i => true).ToList();
            //商品信息
            ViewBag.product = _tProductsService.LoadEntities(i => true).ToList();
            FindHouse(id);
            return PartialView();
        }
        [HttpPost]
        public JsonResult ModuleBuyOrder(t_BuyOrder entity)
        {
            //补充用户和identity
            //entity.UserName = _tUserService.LoadEntities(i => i.Token == Request.Cookies["uid"].Value).First().UserName;
            entity.State = 0;
            entity.Identitys = 0;
            var result = _tBuyOrderService.AddEntity(entity);
            //显示
            return Json(result);
        }
        //查找库房库区信息
        private void FindHouse(int id)
        {

            //查找库房信息
            ViewBag.HouseList = _tStoreHouse.LoadEntities(i => true).ToList();
            //库区信息
            if (id == 0)
            {
                ViewBag.HouseDetail = _tStoreHouseDetail.LoadEntities(i => true).ToList();
            }
            else
            {
                ViewBag.HouseDetail = _tStoreHouseDetail.LoadEntities(i => i.HouseID == id).ToList();
            }
        }


        /// <summary>
        /// 采购入库页面
        /// </summary>
        /// <returns></returns>

        public ActionResult ModuleBuyReceip(int id = 0)
        {
            //查找订单信息
            ViewBag.receipt = _tBuyReceiptService.LoadEntities(i => true).ToList().LastOrDefault();
            //查找订单信息--exec p_BuyReceipt_getAdminBuyReceiptOrderList '2009-06-06','2018-15-15','0'
            ViewBag.data = _tBuyOrderService.Procedures<p_BuyReceipt_getAdminBuyReceiptOrderList_Result>("exec p_BuyReceipt_getAdminBuyReceiptOrderList '2009-01-01','2018-11-15','0'");
            //库区库房
            FindHouse(id);
            //入库信息查询
            return PartialView();
        }
        /// <summary>
        /// 添加采购入库信息
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult ModuleBuyReceip(t_BuyReceipt entity)
        {
            //补充用户和总金额
            var uid = Request.Cookies["uid"].Value;
            entity.UserName = _tUserService.LoadEntities(i => i.Token == uid).First().UserName;
            entity.TotalPrice = _tBuyOrderService.LoadEntities(i => i.BuyOrderID == entity.BuyOrderID).FirstOrDefault().TotalPrice;
            entity.Identitys = 0;
            entity.AlreadyPay = 0;
            //信息添加
            var result = _tBuyReceiptService.AddEntity(entity);
            //显示
            return Json(result);
        }

        //[HttpPost]
        //public JsonResult ModuleBuyReceip(string BuyOrderID) {
        //var result=from a in t_AppendStockDetail
        //           join
        //}
        /// <summary>
        /// 采购付款页面
        /// </summary>
        /// <returns></returns>

        public ActionResult ModuleBuyPay(string id = "0")
        {
            if (id != "0")
            {
                ViewBag.Pay = _tBuyReceiptService.LoadEntities(i => i.ReceiptOrderID == id).FirstOrDefault();
            }
            ViewBag.Payreceipt = _tBuyReceiptService.LoadEntities(i => true).ToList();
            return PartialView();
        }
        [HttpPost]
        public ActionResult ModuleBuyPay(t_BuyPay entity)
        {
            var uid = Request.Cookies["uid"].Value;
            entity.UserName = _tUserService.LoadEntities(i => i.Token == uid).First().UserName;
            var result = _tBuyPayService.AddEntity(entity);
            return Json(result);
        }

        /// <summary>
        /// 采购退货页面
        /// </summary>
        /// <returns></returns>

        public ActionResult ModuleBuyReturn()
        {
            ViewBag.Return = _tBuyReturnService.LoadEntities(i => true).ToList().LastOrDefault();
            ViewBag.data = _tBuyOrderService.Procedures<p_BuyReceipt_getAdminBuyReceiptOrderList_Result>("exec p_BuyReceipt_getAdminBuyReceiptOrderList '2009-01-01','2018-11-15','0'");
            //查找订单信息
            ViewBag.receipt = _tBuyReceiptService.LoadEntities(i => true).ToList().LastOrDefault();
            //库区库房
            ViewBag.HouseList = _tStoreHouse.LoadEntities(i => true).ToList();
            ViewBag.HouseDetail = _tStoreHouseDetail.LoadEntities(i => true).ToList();
            return PartialView();
        }
        [HttpPost]
        public JsonResult ModuleBuyReturn(t_BuyReturn entity)
        {
            var uid = Request.Cookies["uid"].Value;
            entity.UserName = _tUserService.LoadEntities(i => i.Token == uid).First().UserName;
            entity.TotalPrice = _tBuyReceiptService.LoadEntities(i => i.ReceiptOrderID == entity.ReceiptOrderID).FirstOrDefault().TotalPrice;
            var result = _tBuyReturnService.AddEntity(entity);
            return Json(result);
        }
    }
}
